<?php
class ArchivesModel extends ModelBase
{
	private static $_tableName = 'archives';
	private static $_addtionTable = 'archives';
	
	public static $flag = array(
		'hot'	=> '热点',
		'top'	=> '置顶',
		'focus'	=> '幻灯',
		'pic'	=> '图片',
	);
	
	public static function add($cid, $title, $memo, $pic='', $flag='', $author='', $priority=0, $isaudit=1)
	{
		if (empty($cid) || empty($title) || empty($memo)){
			return false;
		}
		$priority = $priority ? $priority : time();
		$data = array(
			'title'		=> $title,
			'cid'		=> $cid,
			'pic'		=> $pic,
			'pubdate'	=> time(),
			'priority'	=> $priority,
			'isaudit'	=> $isaudit,
			'author'	=> $author,
			'flag'		=> $flag,
			'isdelete'	=> 1,
		);
		if (!empty($data['pic'])) {
			if (!$data['flag'] || !in_array('pic', $data['flag'])){
				$data['flag'][] = 'pic';
			}
		}
		$data['flag'] = $data['flag'] ? json_encode($data['flag']) : '';
		$aid = self::queryInsert(self::$_tableName, $data, true);
		if (!$aid){
			return false;
		}
		
		$addtion = array(
			'aid' => $aid,
			'memo' => $memo,
		);
		
		if(AddtionModel::add(self::$_addtionTable, $addtion)){
			return $aid;
		}else{
			return false;
		}
	}
	
	public static function update($aid, $data)
	{
		$where = "id='$aid'";
		if (isset($data['flag'])){
			$data['flag'] = is_array($data['flag']) ? json_encode($data['flag']) : '';
		}
		if (isset($data['memo'])){
			$addtion['memo'] = $data['memo'];
			AddtionModel::update(self::$_addtionTable, "aid='{$aid}'", $addtion);
			unset($data['memo']);
		}
		return self::queryUpdate(self::$_tableName, $data, $where);
	}
	
	public static function getList($where='', $page, $pageSize)
	{
		$start = ($page-1) * $pageSize;
		$archives = self::querySelect(self::$_tableName, array('*'), $where, 'priority desc', $start, $pageSize);
		foreach ($archives as &$item){
			$item['url'] = '/a/'.$item['id'].'.html';
		}
		return $archives;
	}
	
	public static function getCount($where)
	{
		$data = self::querySelectSingle(self::$_tableName, array('count(0) num'), $where);
		return isset($data['num']) ? $data['num'] : 0;
	}
	
	public static function get($aid)
	{
		$data = self::querySelectSingle(self::$_tableName, '*', "id=$aid") 
				+ AddtionModel::get(self::$_addtionTable, "aid={$aid}");
		$data['flag'] = $data['flag'] ? json_decode($data['flag'], true) : array();
		$addtion = AddtionModel::get(self::$_addtionTable, $aid);
		$data['memo'] = isset($addtion['memo']) ? $addtion['memo'] : '';
		return $data;
	}
	
	public static function getHot($count=10)
	{
		$data = self::querySelect(self::$_tableName, array('*'), '1', 'hits desc', 0, $count);
		foreach ($data as &$item){
			$item['url'] = '/a/'.$item['id'].'.html';
		}
		return $data;
	}
	
	public static function getHotByCat($cid, $count=10)
	{
		$data = self::querySelect(self::$_tableName, array('*'), "cid=$cid", 'hits desc', 0, $count);
		foreach ($data as &$item){
			$item['url'] = '/a/'.$item['id'].'.html';
		}
		return $data;
	}
	
	public static function getPicArticle($count=10)
	{
		$where = "`pic`!='' and `flag` like '%pic%'";
		$data = self::querySelect(self::$_tableName, array('*'), $where, "id desc", 0, $count);
		foreach ($data as &$item){
			$item['url'] = '/a/'.$item['id'].'.html';
		}
		return $data;
	}
}